package com.example.dao;



import com.example.entity.BookTransaction;
import com.example.utils.DBUtil;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BookTransactionDao {

    public void addTransaction(BookTransaction transaction) {
        String sql = "INSERT INTO book_transactions (student_id, book_id, book_name, transaction_price, transaction_time) " +
                "VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, transaction.getStudentId());
            pstmt.setInt(2, transaction.getBookId());
            pstmt.setString(3, transaction.getBookName());
            pstmt.setBigDecimal(4, transaction.getTransactionPrice());

            pstmt.executeUpdate();
            System.out.println("Transaction added successfully");
        } catch (SQLException e) {
            System.out.println("Error adding transaction: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public void deleteTransaction(int id) {
        String sql = "DELETE FROM book_transactions WHERE id = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<BookTransaction> getAllTransactions() {
        List<BookTransaction> transactions = new ArrayList<>();
        String sql = "SELECT * FROM book_transactions";

        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                transactions.add(extractTransactionFromResultSet(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return transactions;
    }

    private BookTransaction extractTransactionFromResultSet(ResultSet rs) throws SQLException {
        BookTransaction transaction = new BookTransaction();
        transaction.setId(rs.getInt("id"));
        transaction.setStudentId(rs.getInt("student_id"));
        transaction.setBookId(rs.getInt("book_id"));
        transaction.setBookName(rs.getString("book_name"));
        transaction.setTransactionPrice(rs.getBigDecimal("transaction_price"));
        transaction.setTransactionTime(rs.getTimestamp("transaction_time"));
        return transaction;
    }

    public List<BookTransaction> getTransactionsByStudentId(int studentId) {
        List<BookTransaction> transactions = new ArrayList<>();
        String sql = "SELECT * FROM book_transactions WHERE student_id = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, studentId);
            System.out.println("Executing SQL: " + sql + " with studentId: " + studentId);

            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                BookTransaction transaction = extractTransactionFromResultSet(rs);
                transactions.add(transaction);
                System.out.println("Found transaction: " + transaction.getBookName());
            }

            System.out.println("Total transactions found: " + transactions.size());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return transactions;
    }
} 